﻿/// ****************************************************************************
/// Author:	        Robin Zhu
/// Create Date:	2012-4-18
/// Purpose: 		可配置的路由类定义文件
/// ****************************************************************************
/// Modify By		Date			Remark
/// Robin           2012-5-17       增加当传入参数为空时，读取Cookie的机制
/// ****************************************************************************
/// 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Routing;
using System.Web.Mvc;
using System.Collections.Specialized;

namespace RB.Web.Mvc
{
    /// <summary>
    /// 可配置的路由类
    /// </summary>
    public class ConfigurableRoute : Route, IRouteWithArea
    {
        public ConfigurableRoute(string url, IRouteHandler routeHandler) :
            base(url, routeHandler)
        {
        }

        public ConfigurableRoute(string url, RouteValueDictionary defaults, IRouteHandler routeHandler) :
            base(url, defaults, routeHandler)
        {
        }

        public ConfigurableRoute(string url, RouteValueDictionary defaults,
            RouteValueDictionary constraints, IRouteHandler routeHandler)
            : base(url, defaults, constraints, routeHandler)
        {
        }

        public ConfigurableRoute(string url, RouteValueDictionary defaults,
            RouteValueDictionary constraints, RouteValueDictionary dataTokens,
            IRouteHandler routeHandler)
            : base(url, defaults, constraints, dataTokens, routeHandler)
        {
        }

        public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
        {
            if (_passThroughParameters != null)
            {
                var request = requestContext.HttpContext.Request;
                foreach (var parameter in _passThroughParameters)
                {
                    if (!values.ContainsKey(parameter))
                    {
                        if (!string.IsNullOrEmpty(request[parameter]))
                        {
                            values.Add(parameter, request[parameter]);
                        }
                        else if (_passThroughParameterCookieNames != null &&
                            _passThroughParameterCookieNames.AllKeys.Contains(parameter))
                        {
                            var cookie = request.Cookies[_passThroughParameterCookieNames[parameter]];
                            if (cookie != null)
                            {
                                values.Add(parameter, cookie.Values[parameter]);
                            }
                        }
                    }
                }
            }
            if (_autoTimestampParameter)
            {
                if (values.Keys.Contains("timestamp"))
                {
                    values["timestamp"] = DateTime.Now.Ticks;
                }
                else
                {
                    values.Add("timestamp", DateTime.Now.Ticks);
                }
            }
            
            return base.GetVirtualPath(requestContext, values);
        }

        #region IRouteWithArea 成员

        private string _area = "";

        public string Area
        {
            get { return _area; }
            set { _area = value; }
        }

        #endregion

        private string[] _passThroughParameters;

        public string[] PassThroughParameters
        {
            get { return _passThroughParameters; }
            set { _passThroughParameters = value; }
        }

        private bool _autoTimestampParameter = false;

        public bool AutoTimestampParameter
        {
            get { return _autoTimestampParameter; }
            set { _autoTimestampParameter = value; }
        }

        private NameValueCollection _passThroughParameterCookieNames = null;

        public NameValueCollection PassThroughParameterCookieNames
        {
            get
            {
                return _passThroughParameterCookieNames;
            }
            set
            {
                _passThroughParameterCookieNames = value;
            }
        }
    }
}
